package pkg

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

type Goods struct {
	Id             int    `db:"id"`
	GoodsName      string `db:"goods_name"`
	GoodsPrice     int    `db:"goods_price"`
	GoodsStock     int    `db:"goods_stock"`
	GoodsUrl       string `db:"goods_url"`
	GoodsIntroduce string `db:"goods_introduce"`
	GoodsShing     int    `db:"goods_shing"`
	GoodsStatus    int    `db:"goods_status"`
	GoodsSort      int    `db:"goods_sort"`
}

type Place struct {
	Country string `db:"country"`
	City    string `db:"city"`
	TelCode int    `db:"telcode"`
}

var Db *sqlx.DB

func init() {
	database, err := sqlx.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/week")
	if err != nil {
		fmt.Println("open mysql failed,", err)
		return
	}
	Db = database
}

func MySQLSQLx() {
	conn, err := Db.Begin()
	if err != nil {
		fmt.Println("begin failed :", err)
		return
	}

	r, err := conn.Exec("insert into goods(goods_name, goods_price, goods_stock, goods_url, goods_introduce, goods_shing, goods_status, goods_sort)values(?, ?, ?, ?, ?, ?, ?)", "椅子", "56", "789", "https://sudyoss.oss-cn-beijing.aliyuncs.com/2024-11-082024-11-08.webp", "34", "1", "3548")
	if err != nil {
		fmt.Println("exec failed, ", err)
		conn.Rollback()
		return
	}
	id, err := r.LastInsertId()
	if err != nil {
		fmt.Println("exec failed, ", err)
		conn.Rollback()
		return
	}
	fmt.Println("insert succ:", id)

	r, err = conn.Exec("insert into goods(goods_name, goods_price, goods_stock, goods_url, goods_introduce, goods_shing, goods_status, goods_sort)values(?, ?, ?, ?, ?, ?, ?)", "酸梅汁", "56", "789", "hhhh", "34", "1", "3548")
	if err != nil {
		fmt.Println("exec failed, ", err)
		conn.Rollback()
		return
	}
	id, err = r.LastInsertId()
	if err != nil {
		fmt.Println("exec failed, ", err)
		conn.Rollback()
		return
	}
	fmt.Println("insert succ:", id)

	conn.Commit()
}
